package com.team.kaidb.act;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Toast;

import com.team.kaidb.BaseActivity;
import com.team.kaidb.R;
import com.team.kaidb.bean.request.IRequestBean;
import com.team.kaidb.bean.request.LoginRequestBean;
import com.team.kaidb.customview.LabelInputView;
import com.team.kaidb.presenter.impl.LoginPresenterImpl;
import com.team.kaidb.ui.IViewLogin;
import com.team.kaidb.utils.CheckUtil;
import com.team.kaidb.utils.SPUtils;
import com.team.kaidb.utils.T;

import butterknife.InjectView;
import butterknife.OnClick;

/**
 * 登陆页面
 */
public class ActivityLogin extends BaseActivity implements IViewLogin {

    @InjectView(R.id.input_user_no)
    LabelInputView labelInputView_no;
    @InjectView(R.id.input_user_name)
    LabelInputView labelInputView_name;
    @InjectView(R.id.input_user_pass)
    LabelInputView labelInputView_pass;
    @InjectView(R.id.checkBox)
    CheckBox mCheckBox;


    private LoginPresenterImpl presenterActLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        presenterActLogin = new LoginPresenterImpl(this);

        setDefault();
    }

    /**
     * 设置默认
     */
    private void setDefault() {
        labelInputView_name.setInputType(InputType.TYPE_CLASS_TEXT);
        labelInputView_no.setInputType(InputType.TYPE_CLASS_NUMBER);
        LoginRequestBean bean = (LoginRequestBean) SPUtils.getInstance().getLoginBean();
        if (bean != null) {
            labelInputView_no.setText("" + bean.getMchCaNo());
            labelInputView_name.setText("" + bean.getMchId());
        }
        mCheckBox.setChecked(SPUtils.getInstance().getSavePass());
        if (SPUtils.getInstance().getSavePass()) {
            labelInputView_pass.setText(bean.getPassword());
        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.act_login_lay;
    }

    @OnClick({R.id.button_login, R.id.button_exit})
    void button_click(View view) {
        switch (view.getId()) {
            case R.id.button_login:
                login();
                break;
            case R.id.button_exit:
                finish();
                break;
            default:
                break;
        }
    }

    private void login() {
        if (CheckUtil.isEmpty(labelInputView_no) || CheckUtil.isEmpty(labelInputView_name) || CheckUtil.isEmpty(labelInputView_pass)) {
            T.showShort("不能为空", context);
            return;
        }
        LoginRequestBean requestBean = new LoginRequestBean();
        requestBean.setMchCaNo(labelInputView_no.getText());
        requestBean.setMchId(labelInputView_name.getText());
        requestBean.setPassword(labelInputView_pass.getText());
        SPUtils.getInstance().setLoginBean(requestBean);
        SPUtils.getInstance().setSavePass(mCheckBox.isChecked());
        presenterActLogin.login(requestBean, context);
    }


    @Override
    public void loginSuccess(Object result) {
        startActivity(new Intent(context, ActivityHome.class));
    }

    @Override
    public void loginFaild(String faild) {
        T.showShort("" + faild, context);
    }
}
